//
//  MovieDetailViewController.m
//  EasyToo
//
//  Created by Jr Lan on 13-8-31.
//
//

#import "MovieDetailViewController.h"
#import "DeviceUtil.h"
#import "CinemaListViewController.h"


@interface MovieDetailViewController ()

@end

@implementation MovieDetailViewController
{
    NSMutableArray *_movieInfoArray;
}

- (void) dealloc
{
    [_movieInfoArray release];
    _movieInfoArray = nil;
    
    [super dealloc];
}

@synthesize movieInfo;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    //初始化视图
    [self init_view];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


//初始化视图
- (void) init_view
{
    self.title = @"电影票";
    //加载背影
    [self.view setBackgroundColor:BackgroundColor];

    //加载标题
    UILabel *titlelbl = [[UILabel alloc]initWithFrame:CGRectMake(20, ADJUST_IOS7STATUES_HIGHT+70, 280, 40)];
    titlelbl.font = [UIFont systemFontOfSize:20];
    titlelbl.textColor = [UIColor redColor];
    titlelbl.backgroundColor = [UIColor clearColor];
    titlelbl.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:titlelbl];
    
    //加载详情
    NSMutableArray *textarray = [[NSMutableArray alloc]initWithObjects:@"主演：",@"类型：",@"片长：",@"语言：",@"特效：",nil];
    
    //加载电影描述
    NSString *tempstr = @"本电影暂无简介。本电影暂无简介。本电影暂无简介。本电影暂无简介。本电影暂无简介。本电影暂无简介。本电影暂无简介。本电影暂无简介。本电影暂无简介。本电影暂无简介。本电影暂无简介。本电影暂无简介。本电影暂无简介。本电影暂无简介。本电影暂无简介。本电影暂无简介。";
    [self initDesc:tempstr];
    
    //进入下一步
    UIButton *nextStepBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    nextStepBtn.frame = CGRectMake(20, ADJUST_IOS7STATUES_HIGHT+360, 280, 30);
    [nextStepBtn setBackgroundImage:[UIImage imageNamed:@"xyb"] forState:UIControlStateNormal];
    [nextStepBtn setTitle:@"立即购买" forState:UIControlStateNormal];
    [self.view addSubview:nextStepBtn];
    [nextStepBtn addTarget:self action:@selector(buyEvent) forControlEvents:UIControlEventTouchUpInside];
    
    //数据
    if (movieInfo)
    {
        //电影图像
        UIImageView *imgview  = [[[UIImageView alloc]initWithFrame:CGRectMake(10, ADJUST_IOS7STATUES_HIGHT+120, 100, 150)]autorelease];
        NSURL *photourl = [NSURL URLWithString:movieInfo.image];
        //url请求实在UI主线程中进行的
        UIImage *images = [UIImage imageWithData:[NSData dataWithContentsOfURL:photourl]];//通过网络url获取uiimage
        imgview.image = images;
        [self.view addSubview:imgview];
        
        //电影名字
        titlelbl.text = movieInfo.filmname;
        
        //电影详情
        NSMutableArray *detailarray = [[NSMutableArray alloc]init];
        [detailarray addObject:movieInfo.actor];
        [detailarray addObject:movieInfo.type];
        [detailarray addObject:movieInfo.duration];
        [detailarray addObject:movieInfo.language];
        [detailarray addObject:movieInfo.dimensional];
        
        int i;
        for (i = 0; i < [textarray count]; i ++)
        {
            UILabel *detaillbl;
            if (i == 0) {
                detaillbl = [[[UILabel alloc]initWithFrame:CGRectMake(120, ADJUST_IOS7STATUES_HIGHT+120, 180, 60)]autorelease];
            } else {
                detaillbl = [[[UILabel alloc]initWithFrame:CGRectMake(120, ADJUST_IOS7STATUES_HIGHT+160+22*i, 180, 22)]autorelease];
            }
            //UILabel *detaillbl = [[[UILabel alloc]initWithFrame:CGRectMake(120, 60+30*i, 180, 30)]autorelease];
            detaillbl.font = [UIFont systemFontOfSize:13];
            detaillbl.backgroundColor = [UIColor clearColor];
            detaillbl.adjustsFontSizeToFitWidth = YES;
            detaillbl.lineBreakMode = UILineBreakModeWordWrap;
            detaillbl.numberOfLines = 0;
            [self.view addSubview:detaillbl];
            detaillbl.text = [NSString stringWithFormat:@"%@%@",[textarray objectAtIndex:i],
                              [detailarray objectAtIndex:i]] ;
        }
        
    }
}

//实始化电影描述
- (void)initDesc:(NSString*)tempstr
{
    int length = [self calc_charsetNum:tempstr]/25 + 1;
    
    UIScrollView *myscrollview = [[[UIScrollView alloc] initWithFrame:CGRectMake(10, ADJUST_IOS7STATUES_HIGHT+280, 300, 70)]autorelease];
    myscrollview.directionalLockEnabled = YES; //只能一个方向滑动
    myscrollview.pagingEnabled = NO; //是否翻页
    myscrollview.backgroundColor = [UIColor whiteColor];
    myscrollview.showsVerticalScrollIndicator =YES; //垂直方向的滚动指示
    myscrollview.indicatorStyle = UIScrollViewIndicatorStyleWhite;//滚动指示的风格
    myscrollview.showsHorizontalScrollIndicator = NO;//水平方向的滚动指示
    myscrollview.delegate = self;
    CGSize newSize = CGSizeMake(self.view.frame.size.width, length*15);
    [myscrollview setContentSize:newSize];
    
    //加载电影描述
    
    UILabel *desclbl = [[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 300, length*15)]autorelease];
    desclbl.text = tempstr;
    desclbl.font = [UIFont systemFontOfSize:12];
    desclbl.backgroundColor = [UIColor whiteColor];
    desclbl.adjustsFontSizeToFitWidth = YES;
    desclbl.lineBreakMode = UILineBreakModeWordWrap;
    desclbl.numberOfLines = 0;
    [self.view addSubview:desclbl];
    
    [myscrollview addSubview:desclbl];
    
    [self.view addSubview:myscrollview];
}

//进入下一步订购
- (void)buyEvent
{
    CinemaListViewController *cinemaController = [[CinemaListViewController alloc] init];
    cinemaController.filmId = movieInfo.filmid;
    cinemaController.filmName =  movieInfo.filmname;
    [self.navigationController pushViewController:cinemaController animated:YES];
    [cinemaController release];
}


//计算NSString长度
- (int)calc_charsetNum:(NSString*)_str
{
    unsigned result = 0;
    const char *tchar=[_str UTF8String];
    if (NULL == tchar) {
        return result;
    }
    
    
    for (int i=0; i<strlen(tchar);)
    {
        unsigned t = tchar;
        if( t >= 224 )
            i = i+3;
        else if( t >= 192 )
            i = i+2;
        else 
            i++;
        result++;
    }
    
    return result;
    
}

@end



